package com.sf.dlb.controller;


import com.sf.dlb.config.Global;
import com.sf.dlb.service.ScheduledTaskService;
import com.sf.dlb.service.UserWalletService;
import com.sf.dlb.service.WalletEthAddressService;
import com.sf.dlb.utils.*;
import net.sf.ehcache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统设置相关
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/v1/admin")
public class AdminController {
    private static Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Resource
    private Global global;
    @Autowired
    private ScheduledTaskService scheduledTaskService;
    @Autowired
    private WalletEthAddressService walletEthAddressService;

    /**
     * 手动执行定时任务
     *
     * @returns
     */
    @GetMapping(value = "/scheduledTask")
    @ResponseBody
    public Object scheduledTask() {
        logger.info("scheduledTask start time = "+ DateUtils.getDateTime()+" ");
        scheduledTaskService.handlerUnlockDlb();
        logger.info("scheduledTask start end = "+ DateUtils.getDateTime());
        return ResultVOUtil.success();
    }
    /**
     * 监控url
     *
     * @returns
     */
    @GetMapping(value = "/monitorUrl")
    @ResponseBody
    public Object monitorUrl() {
        logger.info("monitorUrl start time = "+ DateUtils.getDateTime()+" ");
        return ResultVOUtil.success();
    }
    /**
     * 缓存查询
     *
     * @returns
     */
    @GetMapping(value = "/cacheList")
    @ResponseBody
    public Object cacheList() {
        logger.info("cacheList start time = "+ DateUtils.getDateTime()+" ");
        String[] cacheNames=CacheUtils.getCacheManager().getCacheNames();
        List<Map> mapList=new ArrayList<>();
        for (String name:cacheNames){
            Cache cache=CacheUtils.getCacheManager().getCache(name);
            List<String> list=cache.getKeys();
            for (String childName:list) {
                String value= (String) CacheUtils.get(name,childName);
                Map map=new HashMap();
                map.put(name+"->"+childName,value);
                mapList.add(map);
            }

        }
        return ResultVOUtil.success(mapList);
    }
    /**
     * 缓存更新
     *
     * @returns
     */
    @PostMapping(value = "/cacheUpdate")
    @ResponseBody
    public Object cacheUpdate(@RequestBody String json) {
        logger.info("cacheUpdate start time = "+ DateUtils.getDateTime()+" ");
        Map<String,String> map= (Map<String, String>) GsonUtils.getInstance().fromJson(json,Map.class);
        String cacheKey=map.get("cacheKey");
        String cacheValue=map.get("cacheValue");
        CacheUtils.put(cacheKey,cacheValue);
        return ResultVOUtil.success();
    }
    /**
     * 手动生成eth地址
     *
     * @returns
     */
    @GetMapping(value = "/generateAddress")
    @ResponseBody
    public Object generateAddress() {
        logger.info("generateAddress start time = "+ DateUtils.getDateTime()+" ");
        walletEthAddressService.handlerGenerateAddress();
        logger.info("generateAddress end time = "+ DateUtils.getDateTime()+" ");
        return ResultVOUtil.success();
    }
}
